/*
 * @Author: '浪川' '1214391613@qq.com'
 * @Date: 2025-07-05 16:06:20
 * @LastEditors: '浪川' '1214391613@qq.com'
 * @LastEditTime: 2025-08-20 17:09:30
 * @FilePath: \passiflora-edulis-sims\src\stores\CreateBook\puzzle-req-store.ts
 * @Description: 双重拼图 的 状态管理
 *
 * Copyright (c) 2025 by '1214391613@qq.com', All Rights Reserved.
 */
import { defineStore, acceptHMRUpdate } from 'pinia'
import { v1_ai_crossword, v1_ai_double_puzzle } from 'src/boot/PuzzleApi'
import type {
  IAiCrossWordRes,
  IAiWordSearchRes,
  IAiDoublePuzzleRes,
  AiPuzzleReq,
} from 'src/interfaces/Responses/AIRes'
import { isAiDoublePuzzleRes } from 'src/interfaces/Responses/AIRes'

// 创建 ai_puzzle_store
interface IBooKSetting extends AiPuzzleReq {
  data: IAiDoublePuzzleRes[] | IAiWordSearchRes[] | IAiCrossWordRes[] // 返回值
  puzzle_mould: string
}

export const usePuzzleReqStore = defineStore('PuzzleReq', {
  state: (): IBooKSetting => ({
    model: 'deepseek-r1:14b',
    title: '',
    instruction: '',
    puzzle_num: 1,
    puzzle_words_num: 8,
    puzzle_mould: 'DoublePuzzle',
    lang: 'en',
    data: [],
  }),

  getters: {
    ollama_models() {
      return ['deepseek-r1:32b', 'deepseek-r1:14b']
    },
    puzzle_mould_options() {
      return ['DoublePuzzle', 'WordScramble', 'WordSearch', 'CrossWord']
    },
    puzzle_lang_options() {
      return ['zh', 'en', 'jp', 'fr']
    },
  },

  actions: {
    // 请求后端接口，返回AI生成的结果
    async request_v1_ai_create() {
      switch (this.puzzle_mould) {
        case 'DoublePuzzle': {
          const res = await v1_ai_double_puzzle({
            model: this.model,
            title: this.title,
            instruction: this.instruction,
            puzzle_num: this.puzzle_num,
            puzzle_words_num: this.puzzle_words_num,
            lang: this.lang,
          })
          if (res.status_code == 200) {
            this.data = res.data
          }
          return res
        }
        case 'WordScramble': {
          const res = await v1_ai_double_puzzle({
            model: this.model,
            title: this.title,
            instruction: this.instruction,
            puzzle_num: this.puzzle_num,
            puzzle_words_num: this.puzzle_words_num,
            lang: this.lang,
          })
          if (isAiDoublePuzzleRes(res.data))
            for (const db of res.data) {
              db.messages = []
            }
          if (res.status_code == 200) {
            this.data = res.data
          }
          return res
        }

        case 'WordSearch': {
          // 请求接口获取
          const res1 = await v1_ai_double_puzzle({
            model: this.model,
            title: this.title,
            instruction: this.instruction,
            puzzle_num: this.puzzle_num,
            puzzle_words_num: this.puzzle_words_num,
            lang: this.lang,
          })
          // 获取 接口返回的数据
          this.data = res1.data

          break
        }
        case 'CrossWord': {
          const res = await v1_ai_crossword({
            model: this.model,
            title: this.title,
            instruction: this.instruction,
            puzzle_num: this.puzzle_num,
            puzzle_words_num: this.puzzle_words_num,
            lang: this.lang,
          })
          if (res.status_code == 200) {
            this.data = res.data
          }
          return res
        }
        default: {
          const res = {
            status_code: 404,
            message: '无法请求后代，请稍后再试',
            data: [],
            time: 0,
          }
          if (res.status_code == 200) {
            this.data = res.data
          }
          return res
        }
      }
    },
  },
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(usePuzzleReqStore, import.meta.hot))
}
